package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IScenicCommentCacheService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.ScenicCommentStats;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import cn.wolfcode.luowowo.scenic.domain.ScenicComment;
import cn.wolfcode.luowowo.scenic.service.IScenicCommentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StatsDataPersistenceJob {
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyDetailService strategyDetailServices;

    @Reference
    private ITravelStatsCacheService travelStatsCacheService;
    @Reference
    private ITravelService travelService;

    @Reference
    private IScenicCommentCacheService scenicCommentCacheService;
    @Reference
    private IScenicCommentService scenicCommentService;

    /**
     * cron:周期表达式,不能乱系,详情参数教案中给出的博客
     * 0/10****?:从0秒开始计算,每10s执行1次
     */
    @Scheduled(cron = "0/10 * * * * ? ")
    public void persistence() {
        //核心:从redis中查询出数据,然后同步更新到mysql中
        //1.查询所有攻略的热数据
        List<StrategyStats> svos = strategyStatsCacheService.listAllStrategyStats();
        //2.遍历vo对象,封装成StrategyDetail对象
        for (StrategyStats svo : svos) {
            StrategyDetail detail = new StrategyDetail();

            detail.setId(svo.getStrategyId());
            detail.setViewnum(svo.getViewnum());
            detail.setFavornum(svo.getFavornum());
            detail.setReplynum(svo.getReplynum());
            detail.setThumbsupnum(svo.getThumbsupnum());
            detail.setSharenum(svo.getSharenum());

            //3.更新到mysql
            strategyDetailServices.updateStats(detail);
        }
        List<TravelStats> tvos = travelStatsCacheService.listAllTravelStats();
        for (TravelStats tvo : tvos) {
            Travel travel = new Travel();
            travel.setId(tvo.getTravelId());
            travel.setViewnum(tvo.getViewnum());
            travel.setFavornum(tvo.getFavornum());
            travel.setReplynum(tvo.getReplynum());
            travel.setThumbsupnum(tvo.getThumbsupnum());
            travel.setSharenum(tvo.getSharenum());

            travelService.updateState(travel);
        }
        List<ScenicCommentStats> scvos = scenicCommentCacheService.listAllScenicCommentStats();
        for (ScenicCommentStats scvo : scvos) {
            ScenicComment scenicComment = new ScenicComment();
            scenicComment.setThumbupnum(scvo.getThumbsupnum());
            scenicComment.setId(scvo.getCommentId());

            scenicCommentService.updateStats(scenicComment);
        }

    }

    /**
     * cron = "0 0 0 1/1 * ? "
     * 每天0点的时候执行将所有游记当日阅读数设置为0的操作
     */
    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void persistence2() {
        List<TravelStats> tvos = travelStatsCacheService.listAllTravelStats();
        for (TravelStats tvo : tvos) {
            tvo.setTodayviewnum(0);
        }
    }
}
